package com.cryptape.cita_wallet.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import com.cryptape.cita_wallet.bean.MiningOrderIncomeItem;
import com.mangoworld.android.R;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by dengming on 9/3/20.
 */

public class MinerIncomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MiningOrderIncomeItem> datas;
    private CloudMinerAdapter.OnItemClickListener onItemClickListener;

    public MinerIncomeAdapter(List<MiningOrderIncomeItem> datas) {
        this.datas = datas;
    }

    public void setDatas(List<MiningOrderIncomeItem> datas) {
        this.datas = datas;
    }

    public void setOnItemClickListener(CloudMinerAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MinerIncomeAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mining_income, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        ViewHolder incomeHolder = ((ViewHolder) holder);
        MiningOrderIncomeItem bean = datas.get(position);
        String typeName = bean.getTypeName();
        String orderLevelName = bean.getOrderLevelName();
        String name = ObjectUtils.isEmpty(typeName) ? bean.getChannelName() : typeName + "(" + orderLevelName + ")";
        SpannableStringBuilder style = new SpannableStringBuilder(name);
        style.setSpan(new ForegroundColorSpan(Color.BLACK), 0, ObjectUtils.isEmpty(typeName) ? 0 : typeName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.RED), ObjectUtils.isEmpty(typeName) ? 0 : typeName.length(), ObjectUtils.isEmpty(name) ? 0 : name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        incomeHolder.tv_income_type.setText(style);
        incomeHolder.tv_income_time.setText(bean.getCreateTime());
//        if(bean.getType() == 1) {
//            incomeHolder.tv_income_amount.setText("+"+bean.getMoney());
//            incomeHolder.iv_income_type.setBackgroundResource(R.drawable.icon_income);
//        }else
//a<b, 返回-1
//a=b，返回0
//a>b, 返回1
        String money = bean.getMoney();
        BigDecimal bdMoney;
        if (ObjectUtils.isEmpty(money)) {
            bdMoney = BigDecimal.ZERO;
        } else {
            bdMoney = new BigDecimal(money);
        }


        if (bdMoney.compareTo(BigDecimal.ZERO) <= 0) {
            incomeHolder.tv_income_amount.setText(money + "");
            incomeHolder.iv_income_type.setBackgroundResource(R.drawable.icon_withdraw);
        } else {
            incomeHolder.tv_income_amount.setText("+" + money);
            incomeHolder.iv_income_type.setBackgroundResource(R.drawable.icon_income);
        }
//        ((ClountMinerViewHolder)holder).btn_buy.setTag(new Integer(position));
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_income_type;
        TextView tv_income_type;
        TextView tv_income_time;
        TextView tv_income_amount;

        public ViewHolder(View view) {
            super(view);
            tv_income_type = view.findViewById(R.id.tv_income_type);
            tv_income_time = view.findViewById(R.id.tv_income_time);
            tv_income_amount = view.findViewById(R.id.tv_income_amount);
            iv_income_type = view.findViewById(R.id.iv_income_type);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
